<template>
  <div>
    <el-table ref="table-body"
              height="450"
              size="mini"
              border
              v-table-fit
              highlight-current-row
              :data="result"
              @row-dblclick="rowDbClick"
              @selection-change="handleSelectionChange"
              @row-click="clickRow"
              :row-class-name="tableRowClassName">
      <el-table-column
        type="selection"
        prop=""
        v-if="isMulti"
        width="55">
      </el-table-column>
      <el-table-column v-for="(column, index) in tableFields" :key="index" :prop="column.prop" :label="column.label" >
      </el-table-column>
    </el-table>
    <el-button v-if="allowCheck == 1" type="primary" style="margin-top: 10px" @click="returnItem">确定</el-button>
  </div>
</template>

<script>
export default {
  name: "index",
  props: {
    tableFields: Array,
    result: Array,
    allowCheck:0,
    searchTerm:'',
    index:String,
    allowdbclick:String,
    isMulti:Boolean
  },
  data(){
    return{
      arr:[]
    }
  },
  methods: {
    returnItem(){
      this.$emit('send',this.index,this.arr)
      this.$emit('close');
    },
    rowDbClick(row,column){
      if(!this.isMulti) {
        this.arr = [];
        this.arr[0] = row;
        this.returnItem();
      }
    },
    handleSelectionChange(rows) {
      this.arr = [];
      this.arr = rows;
    },
    clearMulti() {
      this.$refs["table-body"].clearSelection()
    },
    clearSelect() {
      this.$refs["table-body"].setCurrentRow()
      this.arr = []
    },
    clickRow(row, column, event) {
      if(this.isMulti) {
        this.$refs["table-body"].toggleRowSelection(row)
      } else {
        this.$refs["table-body"].setCurrentRow(row)
        this.arr = []
        this.arr[0] = row
      }
    },
    // 选中背景色
    tableRowClassName({ row, rowIndex }) {
      let color = "";
      let index = this.arr.findIndex((item) => {
        return item === row
      })
      if(index >= 0) {
        color = 'row-selected'
      }
      return color;
    }

  }
}
</script>

<style scoped>
 .el-table >>> .row-selected > td{
    background-color: #F8ECDA !important;
    /*color:red;*/
  }


</style>
